Imports System.Windows.Forms
Imports System.Collections
Imports System.Runtime.InteropServices

Public Class Browse
    Private Shared sRoot As String

    Private Sub New()
        InitializeComponent()
    End Sub

    Public Shared Function Display(ByRef sInit As String) As Browse
        If sRoot Is Nothing Then
            If sInit = "" Or System.IO.Directory.Exists(sInit) = False Then
                MsgBox("Invalid path: " & vbNewLine & sInit)
                Return Nothing
            End If
            sRoot = sInit
            If sRoot.Substring(sRoot.Length - 1) = "\" Then
                sRoot = sRoot.Substring(0, (sRoot.Length - 1))
            End If
            Dim b1 As Browse = New Browse()
            b1.Show()
            Return b1
        End If
        Return Nothing
    End Function

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.Hide()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.Hide()
    End Sub

    Private Sub Browse_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        UTIL.SaveStg("BRWDLG", "Win_l", Me.Left)
        UTIL.SaveStg("BRWDLG", "Win_t", Me.Top)
        UTIL.SaveStg("BRWDLG", "Win_w", Me.Width)
        UTIL.SaveStg("BRWDLG", "Win_h", Me.Height)
        e.Cancel = True
        Me.Hide()
    End Sub

    Private Sub Browse_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Left = UTIL.GetLongStg("BRWDLG", "Win_l", 150)
        Me.Top = UTIL.GetLongStg("BRWDLG", "Win_t", 100)
        Me.Width = UTIL.GetLongStg("BRWDLG", "Win_w", 338)
        Me.Height = UTIL.GetLongStg("BRWDLG", "Win_h", 230)
        Call StartFill()
    End Sub

    Public Sub Reinit(ByVal sNewIn As String)
        If sNewIn.Substring(sNewIn.Length - 1) = "\" Then
            sNewIn = sNewIn.Substring(0, (sNewIn.Length - 1))
        End If
        If sNewIn.ToUpper <> sRoot.ToUpper Then
            If sNewIn = "" Or System.IO.Directory.Exists(sNewIn) = False Then
                MsgBox("Invalid path: " & vbNewLine & sNewIn)
                Return
            End If

            sRoot = sNewIn
            TreeView.Nodes.Clear()
            Call StartFill()
        End If
        Me.Show()
    End Sub

    Private Sub StartFill()
        Try
            Dim desktop As TreeNode = New TreeNode()
            desktop.Text = GetFName(sRoot)
            desktop.Tag = "D"
            desktop.Checked = True
            TreeView.Nodes.Add(desktop)

            Dim subI As TreeNode

            For Each foundDir As String In System.IO.Directory.GetDirectories(sRoot & "\")
                Try
                    subI = New TreeNode()
                    subI.Text = GetFName(foundDir)
                    subI.Tag = "N" ' not expanded folder
                    subI.ImageIndex = 0
                    subI.Checked = True
                    subI.SelectedImageIndex = 0
                    subI.Nodes.Add("")
                    desktop.Nodes.Add(subI)
                Catch
                End Try
            Next

            For Each fileName As String In System.IO.Directory.GetFiles(sRoot & "\")
                Try
                    subI = New TreeNode()
                    subI.Text = GetFName(fileName)
                    subI.ImageIndex = 1
                    subI.Tag = ""
                    subI.Checked = True
                    subI.SelectedImageIndex = 1
                    desktop.Nodes.Add(subI)
                Catch
                End Try
            Next
            desktop.Expand()

        Catch ex As Exception

        End Try
    End Sub

    Private Sub TreeView_BeforeCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView.BeforeCheck
        If MainDlg.iSts <> 0 Then
            e.Cancel = True
        End If
    End Sub

    Private Sub TreeView_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView.BeforeExpand
        If e.Node.Tag = "N" Then
            e.Node.Nodes.Clear()
            Dim sBase As String = GetNodePath(e.Node) & "\"

            Dim subI As TreeNode
            For Each foundDir As String In System.IO.Directory.GetDirectories(sBase)
                Try
                    subI = New TreeNode()
                    subI.Text = GetFName(foundDir)
                    subI.Tag = "N" ' not expanded folder
                    subI.ImageIndex = 0
                    subI.SelectedImageIndex = 0
                    subI.Checked = e.Node.Checked
                    subI.Nodes.Add("")
                    e.Node.Nodes.Add(subI)
                Catch
                End Try
            Next

            For Each fileName As String In System.IO.Directory.GetFiles(sBase)
                Try
                    subI = New TreeNode()
                    subI.Text = GetFName(fileName)
                    subI.ImageIndex = 1
                    subI.Tag = ""
                    subI.SelectedImageIndex = 1
                    subI.Checked = e.Node.Checked
                    e.Node.Nodes.Add(subI)
                Catch
                End Try
            Next

            e.Node.Tag = "E" ' expanded folder
        End If
    End Sub


    Private Sub TreeView_AfterCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView.AfterCheck
        RecSetCkSts(e.Node, e.Node.Checked)
    End Sub


    Public Shared Function GetNodePath(ByRef tNd As System.Windows.Forms.TreeNode) As String
        Dim sRet As String = tNd.Text
        Dim tNdLc As System.Windows.Forms.TreeNode
        tNdLc = tNd.Parent
        While tNdLc.Tag <> "D" ' Desktop
            sRet = tNdLc.Text & "\" & sRet
            tNdLc = tNdLc.Parent
        End While
        sRet = sRoot & "\" & sRet
        Return sRet
    End Function

    Private Function GetFName(ByRef sIn As String) As String
        Dim i As Integer
        i = sIn.LastIndexOf("\")
        If i = -1 Then
            Return sIn
        Else
            Return sIn.Substring(i + 1)
        End If
    End Function

    Private Sub RecSetCkSts(ByRef tNd As System.Windows.Forms.TreeNode, ByVal bCk As Boolean)
        For Each tCrt As System.Windows.Forms.TreeNode In tNd.Nodes
            tCrt.Checked = bCk
            If (tCrt.Tag = "E") Or (tCrt.Tag = "N") Then
                RecSetCkSts(tCrt, bCk)
            End If
        Next
    End Sub

    Private Sub Browse_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Me.TreeView.Width = Me.Size.Width - 24
        Me.TreeView.Height = Me.Size.Height - 76
    End Sub
End Class
